/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.polimi.meteocal.security;

import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.inject.Named;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.enterprise.context.RequestScoped;
import javax.faces.FacesException;
import javax.faces.application.FacesMessage;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.inject.Inject;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;

import com.polimi.meteocal.business.control.UserManager;

/**
 *
 * @author miglie
 */
@Named
@RequestScoped
public class LoginBean {
    
    @Inject
    private Logger logger;
    @EJB
    UserManager um;
    

    private String username;
    private String password;

    public LoginBean() {
    }
    
    @PostConstruct
    public void init(){
    	if(um.getLoggedUser()!=null){
    		String toUrl="/user/calendarPage.xhtml";
    		try {
                FacesContext ctx = FacesContext.getCurrentInstance();

                ExternalContext extContext = ctx.getExternalContext();
                String url = extContext.encodeActionURL(ctx.getApplication().
                                         getViewHandler().getActionURL(ctx, toUrl));

                extContext.redirect(url);
            } catch (IOException e) {
                throw new FacesException(e);
            }
    	}
    }

    public String getUsername() {
        return this.username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return this.password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String login() {
        FacesContext context = FacesContext.getCurrentInstance();
        HttpServletRequest request = (HttpServletRequest) context.getExternalContext().getRequest();
        try {
        	if(um.getLoggedUser()!=null){
        		return "/user/calendarPage";
        	}
            request.login(this.username, this.password);
            ServletContext sContext = request.getSession().getServletContext();
            sContext.setAttribute("justLogged", true);
            return "/user/calendarPage";
        } catch (ServletException e) {
        	if(um.getLoggedUser()!=null){
        		return "/user/calendarPage";
        	}
            context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,"Login Failed","Login Failed"));
            logger.log(Level.SEVERE,"Login Failed");
            return null;
        }
    }
    public String logout() {
    
        FacesContext context = FacesContext.getCurrentInstance();
        HttpServletRequest request = (HttpServletRequest) context.getExternalContext().getRequest();
        ServletContext sContext = request.getSession().getServletContext();
        sContext.setAttribute("userEvents", null);
        request.getSession().invalidate();
        logger.log(Level.INFO, "User Logged out");
        logger.log(Level.INFO, "userEvents to null");
        return "/login?faces-redirect=true";
    }
}
